import { request } from '../request';

export declare type CaptchaType = 'Login';
export declare interface UsrPwdParam {
  username: string;
  password: string;
  captchaId: string;
  captchaVal: string;
}
export declare interface SessionStaff {
  username: string;
  userid: string;
  privileges: [string];
}
export declare interface TokenInfo {
  token: string;
  user: SessionStaff;
}

const SERVICE = 'carbon-auth';

/**
 * 读取验证码
 *
 * @param type 验证码类型
 * @returns 验证码数据
 */
export async function getCaptcha(type: CaptchaType) {
  return (
    await request<any>({
      url: `${SERVICE}/public/captcha/image/base64?type=${type}`,
      method: 'get'
    })
  ).data;
}

/**
 * 用户名密码+图片验证码登录
 *
 * @param params 登录参数
 * @returns 令牌和用户信息
 */
export async function usrpwdLogin(params: UsrPwdParam) {
  return (
    await request<any>({
      url: `${SERVICE}/public/auth/admin/login/usr/pwd`,
      method: 'post',
      data: params
    })
  ).data;
}
